import os
import json
import sys

# 全局变量声明
# 编译警告忽略
s_ignore_warn = ["4251", "4309"]
# 当前库名称与当前库的根目录
s_lib_name_to_path_map = {}
s_dll_export_macro_head_file_name = "#include \"DllExportDefine.h\"\n\n"
s_dll_path = "${PROJECT_SOURCE_DIR}/../bin/Debug"
s_lib_output = "CMAKE_ARCHIVE_OUTPUT_DIRECTORY"
s_vs_outpath = "../../bin"
s_source_code_folder_name = "source"
s_project_name = "ThzSDK"
s_exe_start_type = "WIN32"
s_export = "_EXPORT"
s_file_postfix = "*.*"#"*.cpp *.h *.json *.text *.ui"
s_source_file_postfix = "*.cpp *.h"
s_source_file_variable = "__FILE"
s_qt_env_var = "QT5.12.10_DIR" # qt环境变量的路径
s_solution_global_var = "globalconfig/SolutionGlobalVar.txt" # 全局cmake变量
# json文件所需的关键字段
s_json_file_name = "CMakeConfig.json"
s_cmake_lists = "CMakeLists.txt"
s_enter_char = "\n"
s_document_description = "# 此文件由py 文件自动生成, 请勿手动修改" + s_enter_char + s_enter_char

# json_key
s_project_type = "Project_Type"                                 #工程类型
s_engineering_name = "Project_Name"                             #工程名称
s_engineering_dependency = "Dependency_Project_Name"            #依赖工程名称
s_include_public = "Include_Public"                             #包含Public文件夹
s_include_dir = "Include_Directories"                           #包含文件夹
s_link_library = "Link_Library_Name"                            #链接库名称
s_link_dir       = "Link_Libraries_Directories"                 #链接库路径
s_dependencies_for_build_order = "Dependencies_For_Build_Order" #工程依赖顺序
s_pre_define_macro = "Pre_Define_Macro"                         #预定义宏
s_third_party_target_link_debug = "ThirdParty_Debug_Link_LibName"            #三方库的debug库名称链接
s_third_party_target_link_release = "ThirdParty_Release_Link_LibName"        #三方库的release库名称链接

s_Debug_link_lib          = "debug"
s_Release_link_lib        = "release"                           #debug 与 release 不同模式link的库名称

# Project_Type 对应的value选项，要么Exe要么lib
s_exe_type = "exe"
s_lib_type = "lib"

# lib与exe cmake_list全局字符传
global s_cmake_lists_string
s_cmake_list_files = []

#
def get_string_by_is_empty(str1,str2):
    if len(str1) == 0:
        return str2
    return str1 + "/" + str2

# 用于字符串追加
def string_append(target, source, annotation=""):
    if len(annotation) != 0:
        target += annotation + s_enter_char
    if len(source) != 0:
        target += source + s_enter_char + s_enter_char
    return target


# 写file_source_group的函数
def file_source_group(folder_name_group, folder_name):
    global s_cmake_lists_string
    global s_cmake_list_files

    enter_char = ""
    if len(folder_name_group) != 0:
        enter_char = "/"
    current_path = "${CURRENT_DIR}/" + folder_name_group + enter_char + s_file_postfix
    folder_name_group_list = folder_name_group.split("/")
    file_var_name = ""
    for item in folder_name_group_list:
        file_var_name += item + "_"
    file = "file(GLOB " + file_var_name + s_source_file_variable + " " + current_path + ")\n"
    source_group = "source_group(" + "\"" + folder_name_group + "\"" + " FILES " + "${" + file_var_name + s_source_file_variable + "})"
    s_cmake_lists_string = string_append(s_cmake_lists_string , file + source_group)
    s_cmake_list_files.append("${" + file_var_name + s_source_file_variable + "}")

# 遍历文件夹
def traverse_file_folder(folder_path, folder_name_group, folder_name, file_group_name_list, fun_object=None):
    file_group_name_list.append(folder_name_group)
    if fun_object is not None:
        fun_object(folder_name_group, folder_name)
    current_file_list = os.listdir(folder_path)
    for item in current_file_list:
        file_str = folder_path + "/" + item
        if os.path.isdir(file_str) is True:
            traverse_file_folder(folder_path=file_str,
                                 folder_name_group=get_string_by_is_empty(str1=folder_name_group, str2=item),
                                 folder_name=item,
                                 file_group_name_list=file_group_name_list,
                                 fun_object=fun_object)
        else:
            continue


# 深度优先搜索遍历非public文件夹文件夹
def traverse_file_folder_no_public(folder_path, folder_name_group, folder_name, file_group_name_list, fun_object=None):
    file_group_name_list.append(folder_name_group)
    if fun_object is not None:
        fun_object(folder_name_group, folder_name)
    current_file_list = os.listdir(folder_path)
    for item in current_file_list:
        file_str = folder_path + "/" + item
        if os.path.isdir(file_str) is False:
            continue
        if item != "Public":
            traverse_file_folder_no_public(folder_path=file_str,
                                           folder_name_group=get_string_by_is_empty(str1=folder_name_group, str2=item),
                                           folder_name=item,
                                           file_group_name_list=file_group_name_list,
                                           fun_object=fun_object)


# 深度优先搜索遍历public文件夹下的内容
def traverse_file_folder_only_public(folder_path, folder_name_group, folder_name, file_group_namelist, fun_object=None):
    if fun_object is not None:
        fun_object(folder_name_group, folder_name)
    current_file_list = os.listdir(folder_path)
    for item in current_file_list:
        file_str = folder_path + "/" + item
        if os.path.isdir(file_str) is False:
            continue
        if item == "Public":
            traverse_file_folder(folder_path=file_str,
                                 folder_name_group=get_string_by_is_empty(str1=folder_name_group, str2=item),
                                 folder_name=item,
                                 file_group_name_list=file_group_namelist,
                                 fun_object=fun_object)


# 写入文件
def write_string_to_file(file_str, file_path):
    if len(file_str) != 0:
        with open(file_path, mode='w', encoding="UTF-8") as file:
            # 将字符串写入
            file_str = file_str.replace("\\", "/")
            #file_str = file_str.replace("//","/")
            if file.write(file_str) == 0:
                print(file_path + "文件写入失败")
            file.close()
            return
    print("传入了空字符")


# 生成导入导出宏文件
def create_export_file(lib_name, folder_path):
    mongoexport = lib_name.upper() + s_export 
    file_str = s_dll_export_macro_head_file_name
    file_str += "#ifdef " + lib_name.upper() + "_HOME" + "\n"
    file_str += "    #define " + mongoexport + "  DLL_EXPORT\n"
    file_str += "#else\n"
    file_str += "    #define " + mongoexport + "  DLL_IMPORT\n"
    file_str += "#endif"
    write_string_to_file(file_str, folder_path + "/" + lib_name + "_home.h")

def get_name_with_dir(name):
    return name + "_DIR"

# 全局工程的cmake_lists文件文件,只用来设置变量
def create_project_cmake_file(project_name, folder_path, lib_name_to_path_dict):
    if isinstance(lib_name_to_path_dict, dict) is False:
        print("当前传入对象不是字典，请查看")
        return
    file_str = s_document_description
    cmake_minimum_required = "cmake_minimum_required(VERSION 3.10)"
    file_str = string_append(file_str, cmake_minimum_required, "#cmake适配的最低版本")

    project = "project(" + project_name + ")"
    file_str = string_append(file_str, project, "#工程设置")

    file_str = string_append(file_str, "set(CMAKE_CXX_STANDARD 14)", "#C++标注")

    global s_ignore_warn 
    #编译警告
    compile_warn = ""
    for item in s_ignore_warn:
        compile_warn += "add_compile_options(/wd"+item+")\n"
    file_str = string_append(file_str, compile_warn, "#忽略警告")

    cmake_runtime_output_directory = "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY " + folder_path + "/../bin" + ")\n"
    cmake_runtime_output_directory += "set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY " + folder_path + "/../lib" + ")\n"
    #cmake_runtime_output_directory += "set(LIBRARY_OUTPUT_PATH ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/../lib)\n"

    cmake_runtime_output_directory += "set(CMAKE_BINARY_DIR " + folder_path + "/../build" + ")\n"
    file_str = string_append(file_str, cmake_runtime_output_directory, "#库文件与exe生成在bin目录下")

    file_str = string_append(file_str, "", "#设置所有的路径变量")

    # 用环境变量替代Qt的路径
    file_str = string_append(file_str, "set(QT_DIR $ENV{" + s_qt_env_var +"})")

    for key in lib_name_to_path_dict:
        variable = "set(" + get_name_with_dir(key) + " " + lib_name_to_path_dict[key] + ")"
        file_str = string_append(file_str, variable)

    # 从全部cmake变量定义文件中拷贝变量
    if os.path.isfile(folder_path + "/../" + s_solution_global_var) is True:
        with open(folder_path + "/../" + s_solution_global_var, mode='r', encoding="UTF-8") as file:
            fileStr = file.read()
            file_str = string_append(file_str, fileStr, "#全局cmake变量")
            file.close()

    #设置debug模式下库名称
    debug_postfix_str = "if(NOT DEFINED CMAKE_DEBUG_POSTFIX)\n set(CMAKE_DEBUG_POSTFIX \"D\")\n endif()"
    file_str = string_append(file_str, debug_postfix_str, "#debug模式下库名称")

    # 加入子文件夹
    string_append(file_str, "", "#增加所有的文件夹")
    for key in lib_name_to_path_dict:
        add_subdirectory = "add_subdirectory(${" + get_name_with_dir(key) + "})"
        file_str = string_append(file_str, add_subdirectory)

    write_string_to_file(file_str=file_str, file_path=folder_path + "/" + s_cmake_lists)
    

# 加入头文件，分组，链接等依赖
def add_head_file_group(depend_list, include_public_list, folder_path, lib_name) -> bool:
    global s_cmake_lists_string

    # 写入关于自己工程的文件夹布局
    file_group_name_list = []
    traverse_file_folder(folder_path=folder_path, folder_name_group="", folder_name="",
                         file_group_name_list=file_group_name_list, fun_object=file_source_group)
    # 包含头文件
    include_directories = "include_directories(\n"
    for item in file_group_name_list:
        include_directories += "${CURRENT_DIR}/" + item + "\n"

    # 全依赖
    for depend in depend_list:
        dependence_path = s_lib_name_to_path_map[depend]
        if len(dependence_path) == 0:
            print("当前依赖的工程没被初始化路径" + depend)
            continue
        temp_file_group_name_list = []
        traverse_file_folder_no_public(folder_path=dependence_path, folder_name_group="", folder_name="",
                                       file_group_name_list=temp_file_group_name_list)
        for groupName in temp_file_group_name_list:
            include_directories += "${" + get_name_with_dir(name=depend) + "}/" + groupName + "\n"

    include_directories += ")"
    s_cmake_lists_string = string_append(s_cmake_lists_string, include_directories, "#增加基本依赖")

    # 依赖public
    include_directories = "include_directories(\n"
    temp_list = depend_list + include_public_list
    for publicDecency in temp_list:
        dependence_path = s_lib_name_to_path_map[publicDecency]
        if len(dependence_path) == 0:
            print("当前依赖的工程没被初始化路径" + publicDecency)
            continue
        temp_file_group_name_list = []
        traverse_file_folder_only_public(folder_path=dependence_path, folder_name_group="", folder_name="",
                                         file_group_namelist=temp_file_group_name_list)
        for groupName in temp_file_group_name_list:
            include_directories += "${" + get_name_with_dir(name=publicDecency) + "}/" + groupName + "\n"

    include_directories += ")"
    s_cmake_lists_string = string_append(s_cmake_lists_string, include_directories, "#增加public依赖")
    return True


# 打开json读取json信息创建cmake_lists
def create_cmake_lists_by_jsonfile(project_type, depend_list, include_public_list, link_library_list,
                                   dependencies_for_build_order, folder_path, lib_name, include_dir, link_lib_dir, pre_define_macro,
                                   third_party_target_link_debug, third_party_target_link_release) -> bool:
    global s_cmake_lists_string
    # cmakefile文件的初始化
    s_cmake_lists_string = s_document_description
    s_cmake_lists_string += "set(TARGETNAME " + lib_name +")\n\nset("+"CURRENT_DIR "+"${"+get_name_with_dir(name=lib_name)+"})\n\n"

    # 预定宏
    add_definitions = "add_definitions("
    for item in pre_define_macro:
        add_definitions += "-D" + item + " "
    
    has_home_macro = False
    for item in pre_define_macro:
        if item.find("_HOME") == len(item) - 5:
            has_home_macro = True
            break

    if  has_home_macro is False:
        add_definitions += "-D" + lib_name.upper() + "_HOME "

    add_definitions += ")"

    s_cmake_lists_string = string_append(s_cmake_lists_string, add_definitions, "#预编译宏定义")

    # cmake_list
    global s_cmake_list_files
    # 初始化存放文件的变量列表
    s_cmake_list_files = []
    if project_type == s_exe_type:
        add_head_file_group(depend_list=depend_list, include_public_list=include_public_list,
                            folder_path=folder_path, lib_name=lib_name)
    elif project_type == s_lib_type:
        add_head_file_group(depend_list=depend_list, include_public_list=include_public_list,
                            folder_path=folder_path, lib_name=lib_name)
    else:
        print("到这里不应该又异常")
        return False


    qt_libs = []

    # 链接库名称
    link_lib = "link_libraries(\n"
    template_library_list = link_library_list + depend_list
    for libraryList in template_library_list:
        if libraryList.find("Qt5") == 0 :
            qt_libs.append(libraryList)
        else:
            link_lib += libraryList + "\n"
    link_lib += ")"
    s_cmake_lists_string = string_append(s_cmake_lists_string, link_lib, "#链接lib")

    # 将所有的文件列表统一到一个文件中
    all_file_var = "set(ALL_SOURCE_FILE \n"
    for files in s_cmake_list_files:
        all_file_var += files + "\n"
    all_file_var += ")"
    s_cmake_lists_string = string_append(s_cmake_lists_string, all_file_var, "#所有文件的变量设置")

    all_file_and_moc = "${ALL_SOURCE_FILE}"
    #处理qt5的库
    if len(qt_libs) > 0:
        qt_cmake_list_str = ""
        for item in qt_libs:
            qt_cmake_list_str += "find_package(" + item + " REQUIRED)\n"
        qt_cmake_list_str += "list(APPEND QT_LIB\n"
        for item in qt_libs:
            qt_cmake_list_str += "${" + item +"_LIBRARIES}\n"
        qt_cmake_list_str +=")\n\n# 如果出现ui_xx.h没找到需要修改BuildProject.py文件\n\n"
        # 头文件
        qt_cmake_list_str +="include_directories(\n"
        for item in qt_libs:
            qt_cmake_list_str += "${" + item + "_INCLUDE_DIRS}\n"
        qt_cmake_list_str +=")\n\n"
        # lib
        qt_cmake_list_str +="link_directories(\n${QT_DIR}/lib\n)\n"
        qt_cmake_list_str +="link_libraries(\n${QT_LIB}\n)\n"

        qt_cmake_list_str = string_append(qt_cmake_list_str, "qt5_wrap_cpp(MOC_SRC ${ALL_SOURCE_FILE})", "#qt库依赖")
        s_cmake_lists_string = string_append(s_cmake_lists_string, qt_cmake_list_str, "#qt库依赖")
        all_file_and_moc += " ${MOC_SRC} "


    # 链接库路径
    link_lib_dir.append(s_lib_output)
    link_directories = "link_directories(\n"
    for item in link_lib_dir:
        link_directories += "${" + item +"}\n"
    link_directories += ")"

    s_cmake_lists_string = string_append(s_cmake_lists_string, link_directories, "#链接lib文件夹")

    # 三方头文件
    include_dir_str = "include_directories(\n"
    for item in include_dir:
        include_dir_str += "${" + item + "}\n"
    include_dir_str += ")"
    s_cmake_lists_string = string_append(s_cmake_lists_string, include_dir_str, "#三方库头文件")

    # qrc文件
    # 工程目录下搜索资源文件, 如果没有搜索到任何资源文件，则qt5_add_resources不执行
    qrc_file = "file(GLOB_RECURSE QRC_SOURCE_FILES ${CURRENT_DIR}/*.qrc)\n"
    qrc_file += "IF(QRC_SOURCE_FILES)\n"
    qrc_file += "        qt5_add_resources(QRC_FILES ${QRC_SOURCE_FILES})\n"
    qrc_file += "        SOURCE_GROUP(\"Resource_File\" FILES ${QRC_SOURCE_FILES})\n"
    qrc_file += "ENDIF(QRC_SOURCE_FILES)\n"
    s_cmake_lists_string = string_append(s_cmake_lists_string, qrc_file, "#qrc")

    global s_exe_start_type
    if project_type == s_exe_type:
        # 增加exe
        add_executable = "add_executable(" + "${TARGETNAME} " + s_exe_start_type + " ${QRC_FILES} " + all_file_and_moc + ")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, add_executable, "#add_exectuable")
        # 设置当前为启动项
        set_start_project = "set_property(DIRECTORY ${"+ "CMAKE_SOURCE_DIR" +"}" +" PROPERTY VS_STARTUP_PROJECT "+ lib_name +")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, set_start_project, "#设置当前工程为vs默认的启动项目")
    elif project_type == s_lib_type:
        # add_library
        add_library = "add_library(" + "${TARGETNAME} " + "SHARED " + " ${QRC_FILES} " + all_file_and_moc + ")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, add_library, "#add_lib")
    else:
        print("到这里不应该又异常")
        return False

    # 对全部文件进行检查，如果没有cpp没文件则设置无连接属性
    cpp_check_str =  "file(GLOB_RECURSE CppFiles *.cpp)\n"
    cpp_check_str += "IF(NOT CppFiles)\n"
    cpp_check_str += "      set_target_properties(${TARGETNAME} PROPERTIES LINKER_LANGUAGE CXX  LINK_FLAGS /NOENTRY)\n"
    cpp_check_str += "ENDIF(NOT CppFiles)"
    s_cmake_lists_string = string_append(s_cmake_lists_string, cpp_check_str, "#无cpp检查，设置链接语言属性")

        # 设置debug 与 release 不同三方库的依赖
    if len(third_party_target_link_debug) != 0 :
        debug_release_target_link_str = "target_link_libraries(${TARGETNAME}"
        for item in third_party_target_link_debug :
            debug_release_target_link_str += "\ndebug " + item
        debug_release_target_link_str += ")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, debug_release_target_link_str, "#设置不同模式下的debug链接")

    if len(third_party_target_link_release) != 0:
        debug_release_target_link_str = "target_link_libraries(${TARGETNAME}"
        for item in third_party_target_link_release :
            debug_release_target_link_str += "\noptimized " + item
        debug_release_target_link_str += ")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, debug_release_target_link_str, "#设置不同模式下的release链接")


    # 增加编译顺序
    if len(depend_list) != 0:
        add_dependencies = "add_dependencies(\n${TARGETNAME}"  + "\n"
        for libraryList in depend_list:
            add_dependencies += libraryList + "\n"
        add_dependencies += ")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, add_dependencies, "#强依赖顺序")

    if len(dependencies_for_build_order) != 0:
        add_dependencies = "add_dependencies(\n${TARGETNAME}"  + "\n"
        for libraryList in dependencies_for_build_order:
            add_dependencies += libraryList + "\n"
        add_dependencies += ")"
        s_cmake_lists_string = string_append(s_cmake_lists_string, add_dependencies, "#编译依赖顺序")

    write_string_to_file(file_str=s_cmake_lists_string, file_path=s_lib_name_to_path_map[lib_name] + "/" + s_cmake_lists)
    return True


# 检查cmake文件有效性
def check_cmake_json_valid_and_write_cmake_lists(json_file_path, folder_name):
    with open(json_file_path, encoding="UTF-8") as file:
        res = True
        # 读取
        json_file = json.load(file)
        # 创建exe还是lib
        project_type = json_file.get(s_project_type)
        if project_type == s_exe_type:
            res = False
        elif project_type == s_lib_type:
            # 读取工程名称，用来生成导入导出宏
            engineering_name = json_file.get(s_engineering_name)
            if folder_name == engineering_name:
                None
            else:
                print("当前CMakeJson中工程名称:" + engineering_name + "与当前所在文件夹:" + folder_name + "名称不匹配")
        else:
            print("当前工程类型不正确，请查看cmake_json是否有勿")
            return
        # 工程名称
        project_name = json_file.get(s_engineering_name)
        # 依赖工程
        dependency_list = json_file.get(s_engineering_dependency)
        # 依赖public文件夹文件头文件
        include_public_list = json_file.get(s_include_public)
        # 链接的库
        link_library_list = json_file.get(s_link_library)
        # 编译依赖顺序
        dependencies_for_build_order = json_file.get(s_dependencies_for_build_order)
        # 预定义宏
        pre_define_macro = json_file.get(s_pre_define_macro)
        # 一下主要是为第三方库
        # 包含文件夹
        include_dir = json_file.get(s_include_dir)
        # 保护库路径
        link_lib_dir = json_file.get(s_link_dir)
        # 一个数组，有两个Object对象，分别是debug与release
        third_party_target_link_debug = json_file.get(s_third_party_target_link_debug)
        if third_party_target_link_debug is None :
            third_party_target_link_debug = []
        third_party_target_link_release = json_file.get(s_third_party_target_link_release)
        if third_party_target_link_release is None :
            third_party_target_link_release = []

        file.close()
        # 检测完成后写入CMake_list文件文件
        return create_cmake_lists_by_jsonfile(project_type=project_type,
                                              depend_list=dependency_list,
                                              include_public_list=include_public_list,
                                              link_library_list=link_library_list,
                                              dependencies_for_build_order=dependencies_for_build_order,
                                              folder_path=os.path.dirname(json_file_path),
                                              lib_name=project_name,
                                              include_dir=include_dir,
                                              link_lib_dir=link_lib_dir,
                                              pre_define_macro=pre_define_macro,
                                              third_party_target_link_debug=third_party_target_link_debug,
                                              third_party_target_link_release=third_party_target_link_release) and res
    return False

#获取json中的str
def get_json_str_value_by_key(cmake_json_config_path, key) -> str:
    value = ""
    if os.path.isfile(cmake_json_config_path):
        with open(cmake_json_config_path, encoding="UTF-8") as file:
            # 读取
            json_file = json.load(file)
            tmp_value = json_file.get(key)
            if isinstance(tmp_value, str) is False:
                print("当前key值对应的不是str")
            else:
                value = tmp_value
                if len(value) == 0:
                    print("当前文件没有"+ key + "对应的字段或者字段对应的值为空。路径：" + cmake_json_config_path)
            file.close()
            return value
    print("当前路径不是cmakeconfig.json")
    return value

#获取json中的list
def get_json_str_list_by_key(cmake_json_config_path, key) -> list:
    listvalue = []
    if os.path.isfile(cmake_json_config_path):
        with open(cmake_json_config_path, encoding="UTF-8") as file:
            # 读取
            json_file = json.load(file)
            tmp_list = json_file.get(key)
            if isinstance(tmp_list, list) is False:
                print("当前key值对应的不是列表")
            else:
                listvalue = tmp_list
            file.close()
    print("当前路径不是cmakeconfig.json")
    return listvalue


# 初始化s_libNameToPath
def init_lib_name_to_path_map(current_path, current_path_folder_name, number):
    global s_lib_name_to_path_map
    file_list = os.listdir(current_path)
    for item in file_list:
        path = current_path + "/" + item
        if os.path.isfile(path) and item == s_json_file_name:
            if current_path_folder_name == s_source_code_folder_name:
                if number + 1 >= 2:
                    print("source文件夹名称只能有一个")
                    return
                else:
                    number += 1
            else:
                # 除了source目录 以外都是工程要么是exe要么是lib
                project_name = get_json_str_value_by_key(cmake_json_config_path = path, key=s_engineering_name)
                if len(project_name) != 0:
                    s_lib_name_to_path_map[project_name] = current_path
            continue
        if os.path.isdir(path):
            init_lib_name_to_path_map(current_path=path, current_path_folder_name=item, number=number)

# 是否需要创建宏定义导入导出文件
def is_need_create_export_file(lib_name , folder_path)->bool:
    current_file_list = os.listdir(folder_path)
    for item in current_file_list:
        file_str = folder_path + "/" + item
        if os.path.isfile(file_str) and item == lib_name + "_home.h" :
            return False
        if os.path.isdir(file_str):
            if is_need_create_export_file(lib_name, file_str) is False:
                return False
    return True


# 遍历创建CMakeLists
def create_cmake_lists(libname_to_path_dict):
    if isinstance(libname_to_path_dict, dict) is False:
        print("当前传入对象不是字典，请查看")
        return
    for key in libname_to_path_dict:
        if check_cmake_json_valid_and_write_cmake_lists(json_file_path=libname_to_path_dict[key] + "/" + s_json_file_name, folder_name=key) is True:
            if is_need_create_export_file(lib_name = key, folder_path = libname_to_path_dict[key]) is True:
                create_export_file(lib_name=key, folder_path=libname_to_path_dict[key])

# 生成所有库的配置文件
def create_libraries_config_file(fileDirPath, lib_name_to_path_dict):
    fileStr = ""
    for item in lib_name_to_path_dict:
        if get_json_str_value_by_key(lib_name_to_path_dict[item] +"/"+ s_json_file_name, s_project_type) == s_lib_type:
            fileStr += item + "\n"
    filePath = fileDirPath + "/../dataconfig"
    if os.path.exists(filePath) == False:
        os.mkdir(filePath)
    write_string_to_file(fileStr , filePath + "/Libraries.txt")

if __name__ == "__main__":
    filestr = os.path.abspath(__file__)
    current_folder_path = os.path.dirname(filestr)
    # 获取路径
    init_lib_name_to_path_map(current_path=current_folder_path,
                          current_path_folder_name=s_source_code_folder_name, 
                          number=0)
    create_libraries_config_file(fileDirPath = current_folder_path , lib_name_to_path_dict = s_lib_name_to_path_map)

    if len(sys.argv) >= 2:
        s_project_name = sys.argv[1]
        if len(sys.argv) >= 3:
            if sys.argv[2] == "Console" :
                s_exe_start_type = ""
            else:
                s_exe_start_type = "WIN32"
    create_project_cmake_file(project_name=s_project_name, 
                              folder_path=current_folder_path, 
                              lib_name_to_path_dict=s_lib_name_to_path_map)
    create_cmake_lists(libname_to_path_dict=s_lib_name_to_path_map)
    print("success")
